var app = getApp()


Page({

    /**
     * 页面的初始数据
     */
    data: {
        data: '',
        status: 0,
        id: '',
        isShow: false,
        delete_id:0,
        address_type:'default'
    },
    showToast: function(e) { //方法
        var that = this
        that.setData({
            isShow: true,
            text: e
        })
        setTimeout(function() {
            that.setData({
                isShow: false
            })
        }, 500)
    },
    // 切换 radio 时 将地址设置默认
    radioChange(e){
        var self = this;
        var list = [];
        var id = e.detail.value;

        var data = this.data.data;
       wx.setStorageSync('addr_id', id)
        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + '/personal/set_def_address',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id: id,
                    },
                    success: function (res) {
                        data.map(function(item,index){
                            if (item.id == id){
                                item.status = 1;
                            }else{
                                item.status = 0;
                            }
                            list.push(item);
                        })
                        self.setData({
                            data: list
                        })
                        wx.showToast({
                            title: '修改成功',
                        })
                        if (self.data.address_type == "select"){
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
            }
        })
    },
    // 设置默认
    // pressView: function(e) {
    //     var id = e.target.dataset;
    // },
    // 选择地址
    checkboxChange: function (e) {
        
    },
    // 删除地址
    dell: function(e) {
        this.dialog.showDialog();
        this.setData({ delete_id: e.target.dataset.id})
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.dialog = this.selectComponent("#dialog");

        
		this.setData({
			address_type: options.type ? options.type : "",
		})
    },
	onShow(){
		this.getAddressList()
	},
	getAddressList(){
		var that = this;
		wx.getStorage({
			key: 'user',
			success: function (res) {
				// 获取地址列表
				wx.request({
					url: app.url.requestUrl + '/personal/addresslist',
					method: 'POST',
					data: {
						openid: res.data.openid,
						token: res.data.token,
					},
					success: function (res) {
						that.setData({
							data: res.data.data,
						})
					}
				})
			},
		})
	},
    // 取消按钮
    __cancelEvent() {
        this.dialog.hideDialog()
    },
    // 确认按钮
    __confirmEvent() {
        var delete_id = this.data.delete_id;
        var addressList = this.data.data;
        var self = this;
        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + '/personal/del_address',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id: delete_id,
                    },
                    success: function (res) {
                        var msg = res.data.msg
                        self.dialog.hideDialog();
                        if(res.data.code == 1){
                            wx.showToast({
                                title: msg,
                            })
                            addressList.map(function(item,index){
                                if (item.id == delete_id){
                                    addressList.splice(index,1);
                                }
                            })
                            self.setData({
                                data: addressList,
                            })
                        }else{
                            wx.showToast({
                                image:"../img/warning.png",
                                title: msg,
                            })
                        }
                    }
                })
            }
        })
    }
})